/**
 * Created by wangxinxin on 2019/2/18.
 */
import React from 'react';
import { Form, Input, Button, message } from 'antd';

import Cookie from './Cookie';

function Login(props) {
  const handleSubmit = e => {
    e.preventDefault();
    props.form.validateFields((err, values) => {
      if (!err) {
        console.log('Received values of form: ', values);
        fetch('http://127.0.0.1:3001/signin', {
          credentials: 'include',
          method: 'POST',
          // headers: {'Content-Type': 'application/json'},
          body: JSON.stringify(values)
        }).then(response => response.json())
          .then(data => {
            if(data.status == 200) {
              Cookie.set('name', data.data.cookie);
              message.success('登录成功');
              props.history.push('/room');
            }
          })
          .catch(error => console.log('error is', error));

      }
    });
  }
  const { getFieldDecorator } = props.form;
  return (
    <Form onSubmit={handleSubmit} className="login-form">
      <Form.Item>
        {getFieldDecorator('userName', {
          rules: [{ required: true, message: 'Please input your username!' }],
        })(
          <Input placeholder="Username" />
        )}
      </Form.Item>
      <Button type="primary" htmlType="submit" className="login-form-button">
        Log in
      </Button>
    </Form>
  )
}
export default Form.create()(Login);